/*
*	File: BinaryFile.cpp
*	Date: 12/07/2011
*	Author: Ivan Gao
*	Desc:
*/
#include "BinaryFile.h"

bool SBinaryFile::ReadBytes(ifstream & file, char * pBuf, int nLen)
{
	file.read (pBuf, (size_t)nLen);
	return !file ? false : true;
} // ReadBytes

bool SBinaryFile::ReadShort (ifstream & file, short & sValue)
{
	file.read((char *)&sValue, sizeof (short));
	return !file ? false : true;
} // ReadShort

bool SBinaryFile::ReadInteger (ifstream & file, int & nValue)
{
	file.read ((char *)&nValue, sizeof (int));
	return !file ? false : true;
} // ReadInteger

bool SBinaryFile::ReadFloat (ifstream & file, float & fValue)
{
	file.read ((char *)&fValue, sizeof (int));
	return !file ? false : true;
} // ReadFloat

bool SBinaryFile::ReadDouble(ifstream & file, double & dValue)
{
	file.read ((char *)&dValue, sizeof (double));
	return !file ? false : true;
} // ReadDouble

bool SBinaryFile::ReadString (ifstream & file, string & str)
{
	int nLen = 0;
	file.read ((char *)&nLen, sizeof (int));
	if (!file || nLen <= 0)
	{
		str.clear ();
		return false;
	}
	static char szTmp[1024 * 10];
	file.read (szTmp, nLen);
	str = szTmp;
	return !file ? false : true;
} // ReadString

bool SBinaryFile::ReadBytes (const char * pInput, char * pBuf, int nLen)
{
	memcpy (pBuf, pInput, nLen);
	return true;
} // ReadBytes

bool SBinaryFile::ReadShort (const char * pInput, short & sValue)
{
	memcpy (&sValue, pInput, sizeof (float));
	return true;
} // ReadShort

bool SBinaryFile::ReadInteger (const char * pInput, int & nValue)
{
	memcpy (&nValue, pInput, sizeof (int));
	return true;
} // ReadInteger

bool SBinaryFile::ReadFloat (const char * pInput, float & fValue)
{
	memcpy (&fValue, pInput, sizeof (float));
	return true;
} // ReadFloat

bool SBinaryFile::ReadDouble (const char * pInput, double & dValue)
{
	memcpy (&dValue, pInput, sizeof (double));
	return true;
} // ReadDouble

bool SBinaryFile::ReadString(const char * pInput, string & str)
{
	int nLen = 0;
	memcpy (&nLen, pInput, sizeof(int));
	if (nLen <= 0)
	{
		return false;
	} // if
	static char szTmp[1024 * 10];
	memcpy (szTmp, (char *)(pInput + 4), nLen);
	str = szTmp;
	return true;
} // ReadString

bool SBinaryFile::WriteBytes (ofstream & file, const char * pBuf, int nLen)
{
	file.write (pBuf, nLen);
	return !file ? false : true;
} // WriteBytes

bool SBinaryFile::WriteShort (ofstream & file, const short sValue)
{
	file.write (reinterpret_cast<const char *>(&sValue), sizeof (short));
	return !file ? false : true;
} // WriteShort

bool SBinaryFile::WriteInteger (ofstream & file, const int nValue)
{
	file.write (reinterpret_cast<const char *>(&nValue), sizeof (int));
	return !file ? false : true;
} // WriteInteger

bool SBinaryFile::WriteFloat (ofstream & file, const float fValue)
{
	file.write (reinterpret_cast<const char *>(&fValue), sizeof (float));
	return !file ? false : true;
} // WriteFloat

bool SBinaryFile::WriteDouble (ofstream & file, const double dValue)
{
	file.write (reinterpret_cast<const char *>(&dValue), sizeof (double));
	return !file ? false : true;
} // WriteDouble

bool SBinaryFile::WriteString (ofstream & file, const string & str)
{
	int nLen = str.size ();
	file.write (reinterpret_cast<const char *>(&nLen), sizeof (int));
	file.write(str.c_str (), nLen);
	return !file ? false : true;
} // WriteString